//
//  MyClassViewController.swift
//  scfriend
//
//  Created by wulanzhou on 2017/7/18.
//  Copyright © 2017年 wulanzhou. All rights reserved.
//

import UIKit

class MyClassViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
    
    
    
    //MAKE:懒加载
    private lazy var myTable:UITableView = {
    
        let tab:UITableView = UITableView.init(frame: self.view.bounds, style:.plain)
        tab.delegate = self
        tab.dataSource = self
        tab.register(ClassTableViewCell.self, forCellReuseIdentifier: "classItem")
        tab.backgroundColor = UIColor.withHex(hexString: "f0f0f0")
        tab.separatorStyle = .none
        tab.rowHeight = 98.0
        return tab
    
    }()
    
    private lazy var sourceData:NSMutableArray = {
    
        let mul:NSMutableArray = NSMutableArray.init(capacity: 0)
        return mul
    }()
    
    private  var classViewModel:ClassesViewModel = ClassesViewModel()

    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.title = "您的班级"
        //添加
        self.navigationItem.rightBarButtonItem = UIBarButtonItem.init(image: UIImage.init(named: "class_add"), style: .plain, target: self, action: #selector(createClassClick(sender:event:)))
        //self.navigationItem.title = "您的班级"
        //头像
        self.navigationItem.leftBarButtonItem = UIBarButtonItem.init(image: UIImage.init(named: "nav_usercenter_icon"), style: .plain, target: self, action: #selector(personCenterClick(sender:event:)))

        self.view.addSubview(self.myTable)
        //加载数据源
        self.classViewModel.loadClassesList(userId: "2",completion: { () in
            
                DispatchQueue.main.async {
                   print("请求完成")
                   self.myTable.reloadData()
                }
        })

        // Do any additional setup after loading the view.
    }
    
    //个人中心
    @objc private func personCenterClick(sender:UIBarButtonItem,event:UIEvent){
    
        let meController:MeViewController = MeViewController()
        self.navigationController?.pushViewController(meController, animated: true)
        
    }
    
    //创建班级
    @objc private func createClassClick(sender:UIBarButtonItem,event:UIEvent){
        
        SCPopOverMenu.showFromEvent(event: event, menuArray: ["创建班级","加入班级","扫一扫"], imageArray: ["menu_addclass","menu_joinclass","menu_scan_image"], doneBlock: { (index:NSInteger) in
            print("SCPopOverMenu index=\(index)")
        }) { 
            print("SCPopOverMenu diss")
        }
    
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    // MARK: - UITableViewDataSource & UITableViewDelegate
    func numberOfSections(in tableView: UITableView) -> Int {
        return self.classViewModel.data.count
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        if let arr = self.classViewModel.data[section].subjects {
           return arr.count
        }
        return 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cellIdentifier : String = "classItem"
        let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! ClassTableViewCell
        
        cell.accessoryType = .disclosureIndicator
        
        let model:SCClass = self.classViewModel.data[indexPath.section]
        //let entity:SCCourse = (model.subjects?[indexPath.row])!
        cell.cellIndexRow = indexPath.row;
        cell.scClass = model
        return cell
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        
        let model:SCClass = self.classViewModel.data[section]
        
        let header:ClassHeaderView = ClassHeaderView.init(frame: CGRect(x: 0, y: 0, width: self.view.bounds.size.width, height: 60))
        header.setTitle(title: model.name!)
        //设置
        header.sectionSettingClick = { () in
        
            print("点击了设置")
        }
        return header
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 60.0
    }
    
    
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        
        //let model:SCClass = self.classViewModel.data[indexPath.section]
        //let entity:SCCourse = (model.subjects?[indexPath.row])!
        
        let tab = UITabBarController.init()
        let vc = ClassRoomViewController()
        vc.tabBarItem = UITabBarItem.init(title: "课堂", image: UIImage.init(named: "tab_class_room"), selectedImage: UIImage.init(named: "tab_class_room_selected"))
        vc.tabBarItem.tag = 0
        
        let vc1 = ClassServiceViewController()
        vc1.tabBarItem = UITabBarItem.init(title: "班务", image: UIImage.init(named: "tab_class_affair"), selectedImage: UIImage.init(named: "tab_class_affair_selected"))
        vc1.tabBarItem.tag = 1
        
        let vc2 = NearViewController()
        vc2.tabBarItem = UITabBarItem.init(title: "附近", image: UIImage.init(named: "tab_class_near"), selectedImage: UIImage.init(named: "tab_class_near_selected"))
        vc2.tabBarItem.tag = 2
        
        tab.viewControllers = [vc,vc1,vc2]
        
        
        self.navigationController?.pushViewController(tab, animated: true)
    }
    
    
   

}
